package dao

import (
	"fmt"
	"go-demo/common"
	"go-demo/model"
)

func GetAllToken() {
	users, _ := GetAllUser()
	for _, users := range users {
		token, _ := common.GenToken(users.Name)
		common.UsersLoginInfo[token] = users
	}
	fmt.Println(common.UsersLoginInfo)
}

// 注册新用户,传入用户名和密码，返回用户id
func CreateUser(username string, password string) {
	user := model.User{
		Name:     username,
		PassWord: password}
	Db.Create(&user)

}

// 通过用户名查询，返回用户对象
func GetUserByUserName(username string) (model.User, error) {
	var user model.User
	result := Db.Where("name = ?", username).First(&user)
	if result.Error != nil {
		return user, result.Error
	}
	return user, nil
}

// 通过用户id查询用户信息,查询一条信息
func GetUserByID(id int64) (model.User, error) {
	var user model.User
	result := Db.First(&user, id)
	if result.Error != nil {
		return user, result.Error
	}
	return user, nil
}

// 获取所有用户信息
func GetAllUser() ([]model.User, error) {
	var users []model.User
	result := Db.Find(&users)
	if result.Error != nil {
		return users, nil
	}
	return users, nil
}
